<template>
  <div class="recharge-record-container">
    <m-header title="充值记录">
      <span class="header-right" style="color:white;" slot="right" @click="showFilterModal">筛选</span>
    </m-header>
    <div class="body-container">
      <scroller :on-refresh="refresh" :on-infinite="infinite" v-if="!isInit">
        <div class="detail" v-if="recordList.length">
          <div
            class="item"
            :class="{'loaded-data': recordList.length < 5, 'loadmore-data': recordList.length < 10}"
            :key="index"
            v-for="(item, index) in recordList"
          >
            <div class="first-col">
              <div class="method">{{item.pay_type}}</div>
              <div class="dateTime">{{item.create_time}}</div>
            </div>
            <div class="status">{{item.status_name}}</div>
            <div class="money">¥{{item.price}}</div>
          </div>
        </div>
      </scroller>
      <div class="no-datas" v-if="!loading && !recordList.length">
        <img src="../../../assets/imgs/icon-noattention@2x.png" alt>
        <div class="desc">暂无充值记录</div>
      </div>
    </div>
    <div class="filter-modal">
      <x-dialog v-model="showFilter" hide-on-blur>
        <div class="modal-content">
          <div class="title">
            选择类型
            <x-icon class="close-icon" @click="showFilter=false" type="ios-close-outline" size="20"></x-icon>
          </div>
          <div class="selector">
            <div
              @click="changeStatus=index"
              :class="changeStatus===index?'active':''"
              v-for="(item, index) in statusList"
              :key="index"
            >{{item.label}}</div>
          </div>
          <div class="btn" @click="selectFilter">确定</div>
        </div>
      </x-dialog>
    </div>
  </div>
</template>
<script>
import { mapActions } from 'vuex';
import { XDialog, Datetime } from 'vux';

export default {
  components: {
    XDialog,
    Datetime
  },
  data () {
    return {
      isInit: false, // 重置滚动
      pageNum: 1,
      isPull: false, // 下拉
      loading: true,
      statusList: [
        { value: '0', label: '全部' },
        { value: '1', label: '支付成功' },
        { value: '2', label: '待支付' }
      ],
      recordList: [],
      showFilter: false,
      selectedStatus: 0,
      changeStatus: 0
      // currentDate: '2018-12-03'
    };
  },
  methods: {
    ...mapActions({
      getRechargeList: 'mine/getRechargeList'
    }),
    refresh (done) { // 下拉
      this.isPull = true;
      this.pageNum = 1;
      this.loading = true;
      this.getDatas(done);
    },
    infinite (done) { // 上滑
      if (!this.isPull) {
        this.loading = true;
        this.getDatas(done);
      }
    },
    getDatas (done) {
      if (this.loading) {
        this.getRechargeList({
          page: this.pageNum,
          status: this.selectedStatus
        }).then(res => {
          this.loading = false;
          let listData = res.list || [];
          if (this.isInit) {
            this.isInit = false;
          }
          if (listData.length) {
            if (this.isPull) {
              this.isPull = false;
              this.recordList = [];
            }
            this.pageNum += 1;
            const listDatas = this.recordList;
            this.recordList = listDatas.concat(listData);
            if (this.pageNum < res.page.total) {
              done && done(true);
            } else {
              done && done(2); ;
            }
          } else {
            done && done(2);
          }
        });
      }
    },
    showDetail (item) {
      item.showDetail = !item.showDetail;
      this.$forceUpdate();
    },
    showFilterModal () {
      this.changeStatus = this.selectedStatus;
      this.showFilter = true;
    },
    selectFilter () {
      this.selectedStatus = this.changeStatus;
      this.showFilter = false;
      // this.pageNum = 1;
      this.recordList = [];
      this.isInit = true;
      this.refresh();
    }
    // getData () {
    //   this.getRechargeList({ page: 1, status: this.selectedStatus }).then(
    //     res => {
    //       if (res) {
    //         this.recordList = res.list;
    //       }
    //     }
    //   );
    // }
  }
};
</script>
<style lang="less">
.recharge-record-container {
  padding-top: 0.92rem;
  padding-bottom: 0.2rem;
  background: #f7f7f7;
  .body-container {
    // margin-top: 0.92rem;
    position: fixed;
    top: 0.92rem;
    width: 100%;
    bottom: 0;
    .header-right {
      color: #ffffff;
      font-size: 0.3rem;
    }
    .summary {
      padding: 0.13rem 0.2rem;
      .month {
        font-size: 0.28rem;
        color: #333333;
        margin-bottom: 0.08rem;
        .month-wrapper {
          display: inline-block;
          width: 2rem;
        }
        img {
          width: 0.24rem;
          margin-left: 0.08rem;
        }
      }
      .number {
        font-size: 0.22rem;
        color: #999999;
      }
    }
    .detail {
      background: #ffffff;
      .item {
        border-bottom: 0.01rem solid #d2d2d2;
        height: 0.95rem;
        padding: 0 0.2rem;
        display: flex;
        .first-col {
          width: 2rem;
          height: 0.95rem;
          .method {
            margin-top: 0.2rem;
            margin-bottom: 0.08rem;
            font-size: 0.28rem;
            color: #333333;
          }
          .dateTime {
            font-size: 0.22rem;
            color: #999999;
          }
        }
        .status {
          width: 3rem;
          height: 0.95rem;
          line-height: 0.95rem;
          text-align: center;
          font-size: 0.28rem;
          color: #333333;
        }
        .money {
          text-align: right;
          width: 2rem;
          height: 0.95rem;
          line-height: 0.95rem;
          font-size: 0.34rem;
          color: #d44743;
        }
      }
    }

    .no-datas {
      position: fixed;
      width: 100%;
      top: 0.92rem;
      bottom: 0;
      height: 100%;
      text-align: center;
      background: #ffffff;
      img {
        display: block;
        width: 4.15rem;
        height: 3.22rem;
        margin: 0 auto;
        padding-top: 0.8rem;
      }
      .desc {
        margin-top: 0.3rem;
        font-size: 0.22rem;
        color: #919191;
      }
    }
  }
  .filter-modal {
    .modal-content {
      position: fixed;
      left: 0;
      right: 0;
      bottom: 0;
      background: #ffffff;
      height: 3.2rem;
      .title {
        font-size: 0.28rem;
        color: #333333;
        height: 0.8rem;
        line-height: 0.8rem;
        border-bottom: 0.01rem solid #d2d2d2;
        position: relative;
        .close-icon {
          position: absolute;
          top: 0.16rem;
          right: 0.16rem;
        }
      }
      .selector {
        font-size: 0.28rem;
        color: #333333;
        height: 0.8rem;
        padding: 0.3rem 0.2rem;
        div {
          display: inline-block;
          width: 1.8rem;
          height: 0.8rem;
          line-height: 0.8rem;
          border: 0.01rem solid #d2d2d2;
          color: #333333;
          font-size: 0.28rem;
          text-align: center;
          border-radius: 0.06rem;
          &.active {
            color: #ffffff;
            background: #d44743;
          }
          &:nth-child(3n-1) {
            margin-left: 0.1rem;
            margin-right: 0.1rem;
          }
        }
      }
      .btn {
        height: 1rem;
        line-height: 1rem;
        background: #d44743;
        font-size: 0.34rem;
        color: #ffffff;
      }
    }
  }
}
</style>
